<script language="JavaScript">
	function fncMinus()
	{
		 if(isNaN(document.frmsale.txtinputMoney.value) || document.frmsale.txtinputMoney.value == "")
		 {
			alert('Please input Number only.');
			document.frmsale.txtinputMoney.focus();
			return;
		 }
		 var inputMoney=parseFloat(document.frmsale.txtinputMoney.value);
		 var subTotal=parseFloat(document.frmsale.subtotal.value);
		 
		 if(inputMoney >= subTotal){
			 document.frmsale.balance.value=00;
			 document.frmsale.txtleftMoney.value = inputMoney - subTotal;
			 document.frmsale.payment.value = subTotal;
		 }else if(inputMoney < subTotal){
			 document.frmsale.payment.value = inputMoney;
			 document.frmsale.balance.value = subTotal - inputMoney;
			 document.frmsale.txtleftMoney.value=0;
		 }
	}
	function redirect(url){
    	window.location = url;
	}
</script>
<?php
@$strMode = $_POST["tMode"];
@$strID = $_POST["tpd_name"];

	$objConnect = mysql_connect("localhost","root","") or die("Error Connect to Database");
	$objDB = mysql_select_db("db_stock");	


if($strMode == "DELETE")
{
	$strSQL = "DELETE FROM tbl_carts ";
	$strSQL .="WHERE pd_name='$strID'";
	$objQuery = mysql_query($strSQL);
}
if($strMode == "ADD")
{
	$pd_name=$_POST["pd_name"];
	
	$qty=$_POST["qty"];
	$b_rate=$_POST["b_rate"];
	$s_rate=$_POST["s_rate"];
	$total=$_POST["total"];
	$avail=$_POST["avail"];
	
	$sql="SELECT * FROM  tbl_carts WHERE pd_name='$pd_name'";
	$result=mysql_query($sql);
	$row=mysql_fetch_array($result);
	$db_pd_name=$row['pd_name'];
	$oldTotal=$row['total'];
	$oldQty=$row['qty'];
	$newQty=$qty+$oldQty;
	$newTotal=$oldTotal +$total;
	
	if($pd_name==$db_pd_name){
		$sql="UPDATE tbl_carts SET  qty='$newQty',total='$newTotal' WHERE pd_name='$pd_name'";
		$result=mysql_query($sql);
		if($result){
			//pageRedirect("index.php?page=sale_list&action=sales&","This product upated successful..!");
			//header("location:index.php?page=sale_list&action=sales&msgStatus=This product upated successful..!");
		}else{
			//pageRedirect("index.php?page=sale_list&action=sales&","This product upated successful..!");
		}
	}else{
		$strSQL = "INSERT INTO tbl_carts(pd_name,qty,buying_price,selling_price,total,avail) 
				VALUES('$pd_name','$qty','$b_rate','$s_rate','$total','$avail') ";
		$objQuery = mysql_query($strSQL);
	}
}
if($strMode == "UPDATE")
{
	$hpd_name=$_POST["hpd_name"];
	$pd_name=$_POST["pd_name"];
	$qty=$_POST["qty"];
	$b_rate=$_POST["b_rate"];
	$s_rate=$_POST["s_rate"];
	$total=$_POST["total"];
		
	$sql="UPDATE tbl_carts SET pd_name='$pd_name', qty='$qty',buying_price='$b_rate',selling_price='$s_rate',total='$total' WHERE pd_name='$hpd_name'";
	$result=mysql_query($sql);
}

$strSQL = "SELECT * FROM tbl_carts";
$objQuery = mysql_query($strSQL);
$sql_cart = mysql_query($strSQL);
$row_cart = mysql_fetch_array($sql_cart);
if($row_cart['pd_name']!=""){
?>
    <form name="frmsale" id="frmsale" method="post" action="sale_verify.php?control=insert">
        <table width="100%" class="bordered">
          <tr>
            <th > <div align="center">Product</div></th>
            <th > <div align="center">Qty</div></th>
            <th > <div align="center">Buy Rate</div></th>
            <th > <div align="center">Sell Rate</div></th>
            <th > <div align="center">Avail</div></th>
            <th> <div align="center">Total</div></th>
             <th> <div align="center">Action</div></th>
          </tr>
        <?php
	
		while($objResult = mysql_fetch_array($objQuery))
		{
		?>
		  <tr>
			<td><div align="center"><?php echo $objResult["pd_name"];?></div></td>
			<td><?php echo $objResult["qty"];?></td>
			<td><?php echo $objResult["buying_price"];?></td>
			<td><?php echo $objResult["selling_price"];?></td>
			<td><?php echo $objResult["avail"];?></td>
			<td align="right"><?php echo $objResult["total"];?></td>
			<td align="center">
				<a href="JavaScript:ShowEdit('<?php echo $objResult["pd_name"];?>','<?php echo $objResult["pd_name"];?>','<?php echo $objResult["qty"];?>','<?php echo $objResult["buying_price"];?>','<?php echo $objResult["selling_price"];?>','<?php echo $objResult["total"];?>','<?php echo $objResult["avail"];?>','<?php echo "UPDATE";?>')"><img src="images/add_card.png" alt="Edit"  /></a>|
				<a href="JavaScript:ajaxDelete('DELETE','<?php echo $objResult["pd_name"];?>');"><img src="images/delete_card.png"  /></a>
			</td>
		   
		  </tr>
		<?php
		}
		?>
        	<tr align="right">
            	<th colspan="5"><div align="right">Sub Total:</div></th>
          		<th colspan="2"><div align="left">
					<?php 
						$sql_sum=" SELECT SUM(total) AS subTotal FROM tbl_carts";
						$result_sum=mysql_query($sql_sum);
						$row_sum=mysql_fetch_assoc($result_sum);
						echo $row_sum['subTotal'];
					?>
					<?php echo $objResult["total"];?>
                    </div>
				</th>
          </tr>
		</table>
	</form>
<?php
	}//end check cart empty
?>